package com.ruoyi.common.utils.poi;

import cn.hutool.core.map.MapUtil;
import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
import org.apache.poi.util.Units;
import org.apache.poi.xwpf.usermodel.*;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTPageSz;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTSectPr;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.STPageOrientation;

import java.io.IOException;
import java.io.InputStream;
import java.math.BigInteger;
import java.net.URL;
import java.util.List;
import java.util.Map;

/**
 * word导出
 *
 * @author tzx
 */
public class WordUtil {

    /**
     * 设置纸张大小
     *
     * @param document doc对象
     * @param width    宽
     * @param height   长
     */
    public void setPageSize(XWPFDocument document, long width, long height) {
        CTSectPr sectPr = document.getDocument().getBody().addNewSectPr();
        CTPageSz pgsz =  sectPr.addNewPgSz();
//        pgsz.setW(BigInteger.valueOf(width));
//        pgsz.setH(BigInteger.valueOf(height));
    }

    /**
     * 切换纸张方向  强行换页
     *
     * @param paragraph   需要切换的段落
     * @param orientation landscape 竖向
     */
    public void changeOrientation(XWPFParagraph paragraph, String orientation) {
        CTSectPr ctSectPr = paragraph.getCTP().addNewPPr().addNewSectPr();
        CTPageSz pageSize = ctSectPr.isSetPgSz() ? ctSectPr.getPgSz() : ctSectPr.addNewPgSz();
        if (orientation.equals("landscape")) {
            pageSize.setOrient(STPageOrientation.LANDSCAPE);
            pageSize.setW(BigInteger.valueOf(842 * 20));
            pageSize.setH(BigInteger.valueOf(595 * 20));
        } else {
            pageSize.setOrient(STPageOrientation.PORTRAIT);
            pageSize.setH(BigInteger.valueOf(842 * 20));
            pageSize.setW(BigInteger.valueOf(595 * 20));
        }
    }

    /**
     * word 段落设置
     *
     * @param doc      doc
     * @param pa       对齐方式
     * @param text     文本内容
     * @param bold     是否加粗
     * @param size     字体大小
     * @param position 行间距(我测试的正常文本间距为5，标题间距20)
     */
    public void selectParagraph(XWPFDocument doc, ParagraphAlignment pa, String text, Boolean bold, Integer size, Integer position) {
        // 新建一个段落
        XWPFParagraph p = doc.createParagraph();
        // 创建文本
        XWPFRun r = p.createRun();
        //对齐方式
        p.setAlignment(pa);
        //文本内容
        r.setText(text);
        //是否加粗
        r.setBold(bold);
        //字体大小
        r.setFontSize(size);
        //行间距(我测试的正常文本间距为5，标题间距20)
        r.setTextPosition(position);
    }


    /**
     * 设置表格
     *
     * @param doc
     * @param list
     * @param index
     * @param number
     * @return
     */
    public int setTable(XWPFDocument doc, List<Map<String, Object>> list, int index, int number) {
        XWPFTable table = doc.createTable(list.size(), index);
        table.setTableAlignment(TableRowAlign.CENTER);
        // 获取到刚刚插入的行 第一行，下标为0
        int i = 0;
        for (Map<String, Object> map : list) {
            XWPFTableRow row1 = table.getRow(i);
            for (int a = 0; a < index; a++) {
                XWPFTableCell cell = row1.getCell(a);
                cell.setVerticalAlignment(XWPFTableCell.XWPFVertAlign.CENTER);
                XWPFParagraph cellParagraph = cell.getParagraphArray(0);
                cellParagraph.setAlignment(ParagraphAlignment.CENTER);
                cellParagraph.setVerticalAlignment(TextAlignment.CENTER);
                XWPFRun titleFun = cellParagraph.createRun();
                // 设置单元格内容
                titleFun.setText(MapUtil.getStr(map, "idx" + (a + 1)));
                titleFun.setFontSize(5);
                cell.setWidth("1580");
                if (i == 0) {
                    titleFun.setBold(true);
                }
            }
            i++;
        }
        doc.setTable(number, table);
        number = number + 1;
        return number;
    }

    /**
     * 设置表格
     *
     * @param doc
     * @param list
     * @param index
     * @param number
     * @return
     */
    public Integer setTable1(XWPFDocument doc, List<Map<String, Object>> list, int index, int number) {
        XWPFTable table = doc.createTable(list.size(), index);
        table.setTableAlignment(TableRowAlign.CENTER);
        // 获取到刚刚插入的行 第一行，下标为0
        int i = 0;
        for (Map<String, Object> map : list) {
            XWPFTableRow row1 = table.getRow(i);
            for (int a = 0; a < index; a++) {
                XWPFTableCell cell = row1.getCell(a);
                cell.setVerticalAlignment(XWPFTableCell.XWPFVertAlign.CENTER);
                XWPFParagraph cellParagraph = cell.getParagraphArray(0);
                cellParagraph.setAlignment(ParagraphAlignment.CENTER);
                cellParagraph.setVerticalAlignment(TextAlignment.CENTER);
                XWPFRun titleFun = cellParagraph.createRun();
                // 设置单元格内容
                titleFun.setText(MapUtil.getStr(map, "row" + (a + 1)));
                titleFun.setFontSize(5);
                if (a == 0) {
                    cell.setWidth("600");
                    titleFun.setBold(true);
                } else {
                    cell.setWidth("1440");
                }
                if (i == 0) {
                    titleFun.setBold(true);
                }
            }
            i++;
        }
        doc.setTable(number, table);
        number = number + 1;
        return number;
    }

    /**
     * 图片
     */
    public void setPicture(XWPFDocument doc, String fileUrl) throws IOException, InvalidFormatException {
        // 新建一个段落
        XWPFParagraph paragraph = doc.createParagraph();
        XWPFRun run = paragraph.createRun();
        URL url = new URL(fileUrl);
        InputStream inputStream = url.openStream();
        int imageType = XWPFDocument.PICTURE_TYPE_PNG;
        int width = 450;
        int height = 400;
        run.addPicture(inputStream, imageType, "image.png",
            Units.toEMU(width),
            Units.toEMU(height));
    }
}
